#!/bin/bash

MOUNTPOINT="./bvt_tffs"
MOUNTDIR="./bvt_tffs_base"
CONTROLDIR="./bvt_linux"


function umounttffs {
    sudo umount ./bvt_tffs
}

function cleanupbvt {
    rm -rf ./bvt_linux
    rm -rf ./bvt_tffs_base
    rm -rf ./bvt_tffs
}
function failbvt {
    echo $1
    umounttffs
    cleanupbvt
    exit 1
}

function setupbvt {
    mkdir -p $MOUNTPOINT
    mkdir -p $MOUNTDIR
    mkdir -p $CONTROLDIR

    sudo ../src/tffs $MOUNTDIR $MOUNTPOINT -o allow_other
    if [ "$?" -ne "0" ]; then
	echo "Couldn't mount $MOUNTDIR on $MOUNTPOINT"
	cleanupbvt
	exit 1
    fi
}


setupbvt
echo "mounted tffs at $MOUNTPOINT"

# tffs_mkdir     | make a directory
# tffs_opendir   | open a directory
# tffs_readdir   | ls a directory
# tffs_rmdir     | delete a directory
# tffs_releasedir|


out=`mkdir $MOUNTPOINT/testmkdir`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't mkdir $MOUNTPOINT/testmkdir ($out)"
fi

out=`ls -la $MOUNTPOINT/testmkdir`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't ls contents of $MOUNTPOINT/testmkdir ($out)"
fi

out=`rmdir $MOUNTPOINT/testmkdir`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't rmdir contents of $MOUNTPOINT/testmkdir ($out)"
fi

echo "ls -la $MOUNTPOINT/testmkdir"
out=`ls -la $MOUNTPOINT/testmkdir`
if [ "$?" -eq "0" ]; then
    failbvt "directory $MOUNTPOINT/testmkdir was removed, but seems to still exist. ($out)"
fi

# tffs_create    | open a new file
# tffs_open      | open an existing file
# tffs_read      | read a file
# tffs_write     | write a file
# tffs_rename    | move a file

echo "dd if=/dev/urandom of=$CONTROLDIR/f bs=1M count=10"
out=`dd if=/dev/urandom of=$CONTROLDIR/f bs=1M count=10`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't create file on control system!?. ($out)"
fi

echo "dd if=$CONTROLDIR/f of=$MOUNTPOINT/f bs=1M"
out=`dd if=$CONTROLDIR/f of=$MOUNTPOINT/f bs=1M `
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't write a file on tffs. ($out)"
fi

echo "diff $MOUNTPOINT/f $CONTROLDIR/f"
out=`diff $MOUNTPOINT/f $CONTROLDIR/f`
if [ "$?" -ne "0" ]; then
    failbvt "File on tffs seems to have data corruption($out)"
fi

echo "mv $MOUNTPOINT/f $MOUNTPOINT/a"
out=`mv $MOUNTPOINT/f $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "File didn't move correctly ($out)"
fi

echo "ls $MOUNTPOINT/a"
out=`ls $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "file $MOUNTPOINT/a should have been created ($out)"
fi

echo "ls $MOUNTPOINT/f"
out=`ls $MOUNTPOINT/f`
if [ "$?" -eq "0" ]; then
    failbvt "file $MOUNTPOINT/a should have been deleted ($out)"
fi

echo "mv $MOUNTPOINT/a $MOUNTPOINT/f"
out=`mv $MOUNTPOINT/a $MOUNTPOINT/f`
if [ "$?" -ne "0" ]; then
    failbvt "File didn't move correctly ($out)"
fi

echo "ls $MOUNTPOINT/a"
out=`ls $MOUNTPOINT/a`
if [ "$?" -eq "0" ]; then
    failbvt "file $MOUNTPOINT/a should have been deleted ($out)"
fi

echo "ls $MOUNTPOINT/f"
out=`ls $MOUNTPOINT/f`
if [ "$?" -ne "0" ]; then
    failbvt "file $MOUNTPOINT/f should have been created ($out)"
fi

# tffs_truncate  | truncate a file
# tffs_release   | close a file

echo "cp $MOUNTPOINT/f $MOUNTPOINT/a"
out=`cp $MOUNTPOINT/f $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "Can't move $MOUNTPOINT/f to $MOUNTPOINT/a ($out)"
fi

echo "truncate $MOUNTPOINT/a --size=1M"
out=`truncate $MOUNTPOINT/a --size=1M`
if [ "$?" -ne "0" ]; then
    failbvt "Can't truncate $MOUNTPOINT/a ($out)"
fi

echo "ls -lh $MOUNTPOINT/a"
out=`ls -lh $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "Can't ls $MOUNTPOINT/a ($out)"
fi

idx=`expr index "$out" 1.0M`
if [ "$idx" -le "0" ]; then
    failbvt "File is not sized as expected after truncate ($out)"
fi

echo "rm $MOUNTPOINT/a"
out=`rm $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "Can't truncate $MOUNTPOINT/a ($out)"
fi

echo "ls $MOUNTPOINT/a"
out=`ls $MOUNTPOINT/a`
if [ "$?" -eq "0" ]; then
    failbvt "file $MOUNTPOINT/a should have been deleted ($out)"
fi

# tffs_getattr   | stat file
st1=`stat $MOUNTPOINT/f --format="%a:%B:%b:%F:%f:%G:%g:%h:%o:%s:%U:%u:%X:%x:%Y:%y:%Z:%z"`
if [ "$?" -ne "0" ]; then
    failbvt "Can't stat tffs mounted file ($st1)"
fi

st2=`stat $MOUNTDIR/f --format="%a:%B:%b:%F:%f:%G:%g:%h:%o:%s:%U:%u:%X:%x:%Y:%y:%Z:%z"`
if [ "$?" -ne "0" ]; then
    failbvt "Can't stat tffs mounted file ($st2)"
fi

if [ "${st1}" != "${st2}" ]; then
    echo "----------"
    echo ${st1}
    echo "----------"
    echo ${st2}
    echo "----------"

    failbvt "Stats don't line up."
fi

# tffs_chmod     | chmod a file                       |
# tffs_chown     | chown a file                       |
# tffs_chgrp     | chgrp a file                       |
chmod 777 $MOUNTPOINT/f
if [ "$?" -ne "0" ]; then
    failbvt "Can't change permissions on tffs mounted file"
fi

chgrp bin $MOUNTPOINT/f
if [ "$?" -ne "0" ]; then
    failbvt "Can't change group on tffs mounted file"
fi

chown nobody $MOUNTPOINT/f
if [ "$?" -ne "0" ]; then
    failbvt "Can't change owner on tffs mounted file"
fi


out=`stat $MOUNTDIR/f --format="%U:%G:%a"`
if [ "$?" -ne "0" ]; then
    failbvt "Failed to stat $MOUNTDIR/f for permissions checks ($out)"
fi
if [ "$out" != "nobody:bin:777" ]; then
    failbvt "Failed to set permissions or access ($out)"
fi

# tffs_symlink   | create a symlink                   |
# tffs_readlink  |
# tffs_unlink    | unlink a file, symlink             |


echo "ln -s $MOUNTDIR/f $MOUNTDIR/a"
out=`ln -s $MOUNTDIR/f $MOUNTDIR/a`
if [ "$?" -ne "0" ]; then
    failbvt "Failed to link from $MOUNTDIR/f to $MOUNTDIR/a  ($out)"
fi

echo "ls -la $MOUNTPOINT/a"
out=`ls -la $MOUNTPOINT/a`
if [ "$?" -ne "0" ]; then
    failbvt "Can't ls $MOUNTPOINT/a ($out)"
fi

idx=`expr index "$out" "-> $MOUNTPOINT/f"`
if [ "$idx" -le "0" ]; then
    failbvt "Mount doesn't look correct($out)"
fi

echo "unlink $MOUNTDIR/a"
out=`unlink $MOUNTDIR/a`
if [ "$?" -ne "0" ]; then
    failbvt "Failed to link from $MOUNTDIR/f to $MOUNTDIR/a  ($out)"
fi

echo "ls -la $MOUNTPOINT/a"
out=`ls -la $MOUNTPOINT/a`
if [ "$?" -eq "0" ]; then
    failbvt "$MOUNTPOINT/a should have been unlinked($out)"
fi

# tffs_link      | link a file                        |

echo "touch $MOUNTPOINT/b"
out= `touch $MOUNTPOINT/b`
if [ "$?" -ne "0" ]; then
    failbvt "Could write to $MOUNTPOINT/b"
fi

sudo ln $MOUNTPOINT/b $MOUNTPOINT/c
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't ln $MOUNTPOINT/b to $MOUNTPOINT/c ($out)"
fi

out=`stat $MOUNTDIR/b --format="%h"`
if [ "$out" -ne "2" ]; then
    failbvt "link count for $MOUNTDIR/b is off (out)"
fi

out=`stat $MOUNTDIR/c --format="%h"`
if [ "$out" -ne "2" ]; then
    failbvt "link count for $MOUNTDIR/c is off (out)"
fi

echo "unlink $MOUNTPOINT/b"
out=`unlink $MOUNTPOINT/b`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't unlink $MOUNTPOINT/b ($out)"
fi

out=`stat $MOUNTDIR/c --format="%h"`
if [ "$out" -ne "1" ]; then
    failbvt "link count for $MOUNTDIR/c is off (out)"
fi


echo "Breadth coverage done."


echo "More intensive IO tests:"
echo "writing at sizes and strides..."
for i in {1..16}
do 
for j in {0..7}
do 

echo "dd if=/dev/urandom of=$MOUNTDIR/f bs=512 count=$i seek=$j"
out=`dd if=/dev/urandom of=$MOUNTDIR/f bs=512 count=$i seek=$j`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't create file on control system!?. ($out)"
fi

done
done

for i in 512 1024 1536 2048 2560 3072 3584 4096
do 
for j in {0..7}
do 

echo "dd if=/dev/urandom of=$MOUNTDIR/f bs=$i count=1 seek=$j"
out=`dd if=/dev/urandom of=$MOUNTDIR/f bs=$i count=1 seek=$j`
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't create file on control system!?. ($out)"
fi

done
done

echo "Postmark (should complete with no visible errors):"
echo "postmark postmark.txt"
postmark postmark.config
if [ "$?" -ne "0" ]; then
    failbvt "Postmark doesn't seem happy($out)"
fi

echo "Kbench: The kernel compile"
echo "postmark postmark.txt"

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.3.tar.bz2 -O $MOUNTDIR/kernel.tar.bz2
if [ "$?" -ne "0" ]; then
    failbvt "Looks like we failed to download a linux kernel, stale link?"
fi

tar -xjf $MOUNTDIR/kernel.tar.bz2 -C $MOUNTDIR
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't untar the linux kernel"
fi
make -C $MOUNTDIR/linux-2.6.39.3 allnoconfig
if [ "$?" -ne "0" ]; then
    failbvt "Couldn't configure the linux kernel"
fi
make -C $MOUNTDIR/linux-2.6.39.3 -j8
if [ "$?" -ne "0" ]; then
    failbvt "Seems like we failed to build the linux kernel"
fi

echo "IO Correctness: running io correctness test - this takes a long time."
./iocorrect.sh $MOUNTDIR/iotest
if [ "$?" -ne "0" ]; then
    failbvt "We did not pass IO correctness."
fi

#Shutdown
umounttffs
cleanupbvt

echo "---------------"
echo "Test successful"
echo ""

#These aren't being tested yet

# tffs_statvfs   |
# tffs_mknod     |
# tffs_utimens   | change timestamps                  |
# tffs_statvfs   |
# tffs_access    |
# tffs_ftruncate |
# tffs_fgetattr  |
